package com.zqq.user.ui.activity

import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.zqq.baselibrary.ext.enable
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ui.activity.BaseMvpActivity
import com.zqq.provider.router.RouterPath
import com.zqq.user.R
import com.zqq.user.injection.component.DaggerUserComponent
import com.zqq.user.injection.module.UserModule
import com.zqq.user.presenter.ResetPwdPresenter
import com.zqq.user.presenter.view.ResetPwdView
import kotlinx.android.synthetic.main.activity_reset_pwd.*
import org.jetbrains.anko.toast

/**
 * 重置密码界面
 * wc
 */
@Route(path = RouterPath.UserCenter.PATH_RESET_PWD)
class ResetPwdActivity : BaseMvpActivity<ResetPwdPresenter>(), ResetPwdView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_reset_pwd)

        initView()
    }

    override fun injectComponent() {
        DaggerUserComponent.builder()
                .activityComponent(mActivityComponent)
                .userModule(UserModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }

    private fun initView() {
        val mobile = intent.getStringExtra(RouterPath.UserCenter.ROUTE_KEY_FORGET)

        mConfirmBtn.enable(mPwdEt) { isBtnEnable() }
        mConfirmBtn.enable(mPwdConfirmEt){ isBtnEnable()}

        mConfirmBtn.onClick {
            if (mPwdEt.text.toString() != mPwdConfirmEt.text.toString()) {
                toast("密码不一致")
                return@onClick
            }
            mPresenter.resetPwd(mobile, mPwdEt.text.toString())
        }
    }

    //判断按钮是否可用
    private fun isBtnEnable(): Boolean {
        return mPwdEt.text.isNullOrEmpty().not() &&
                mPwdConfirmEt.text.isNullOrEmpty().not()
    }


    /**-----------------接口返回---------------------*****/
    override fun onResetPwdResult(result: String) {
        toast(result)
        ARouter.getInstance()
                .build(RouterPath.UserCenter.PATH_LOGIN)
                .withFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .navigation()

        //https://www.cnblogs.com/baiqiantao/p/10125084.html
    }


}
